package com.xu.sms.controller.admin;

import com.xu.sms.entity.Type;
import com.xu.sms.service.TypeService;
import com.xu.sms.utils.DateUtils;
import com.xu.sms.utils.PageResult;
import com.xu.sms.utils.UUIDUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author xupenghao@163.com
 * @date 2018-06-05 17:33
 */
@Controller
@RequestMapping("/admin/type")
public class AdminTypeController {

    @Resource
    private TypeService typeService;

/*    //处理日期的问题
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sm, true));
    }*/

    @RequestMapping("/addTypeUI")
    public String addTypeUI(Model model){
        List<Type> typeList = typeService.findTypeByparentId(0);
        model.addAttribute("typeList",typeList);
        System.out.println(typeList);
        return "admin/type/type_add";
    }

    @RequestMapping("/descUI")
    public String descUI(Integer typeId,Model model){
        model.addAttribute("typeMap",typeService.findTypeAndParentById(typeId));

        return "/admin/type/type_desc";
    }

    @RequestMapping("/editUI")
    public String editUI(Integer typeId,Model model){
        model.addAttribute("typeList",typeService.findTypeByparentId(0));
        model.addAttribute("typeMap",typeService.findTypeAndParentById(typeId));

        return "/admin/type/type_edit";
    }

    @RequestMapping("/delete")
    public String delete(Integer typeId,Model model){
        typeService.delete(typeId);
        return typeListUI(null,null,null,model);
    }

    @RequestMapping("/edit")
    public String edit(Integer typeId,Model model){

        return typeListUI(null,null,null,model);
    }







    @RequestMapping("/addType")
    public String add(String typeName,Date typeTime,
                      Integer typeParentid,
                      @RequestParam(value = "imgFile")MultipartFile imgFile,
                      String typeDescription,
                      HttpServletRequest request,
                      Model model){
       // String uplodPath = "E:/IdeaJavaProjectEE/sms/target/classes/static/upload/img";
        String uplodPath = "/upload/img/";
        String typeImgurl = null;
        if(imgFile !=null){
            System.out.println(request.getSession().getServletContext().getRealPath("/"));
            System.out.println(request.getServletContext().getRealPath("/"));

            String imgName = imgFile.getOriginalFilename();
            // imgName = UUIDUtils.getUUID()+"_"+imgName;
            File file = new File("."+uplodPath,imgName);

            try {
                byte[] bytes = imgFile.getBytes();
                BufferedOutputStream buffered = new BufferedOutputStream(
                        new FileOutputStream(file));
                buffered.write(bytes);
                buffered.close();
                typeImgurl = uplodPath+imgName;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Type type = new Type(typeName,typeTime,typeImgurl,typeDescription,typeParentid);
        typeService.add(type);
        return typeListUI(null,null,null,model);
    }

    @RequestMapping("/findTypeByParentId")
    @ResponseBody
    public List<Type> findTypeByParentId(int typeParentid){
        List<Type> typeList = typeService.findTypeByparentId(typeParentid);
        return typeList;
    }

    //查询大于当前节日的节日
    @RequestMapping("/findTypeBigCurDate")
    @ResponseBody
    public List<Type> findTypeBigCurDate(){
        String curDateStr = DateUtils.dateToFormatString(new Date(),DateUtils.YYYY_MM_DD);
        List<Type> typeList = typeService.findTypeBigCurDate(curDateStr);
        return typeList;
    }


    @RequestMapping("/typeListUI")
    public String typeListUI(Integer pCur,String typeName,Integer parentId,
                             Model model ){


       model.addAttribute("typePList",typeService.findTypeByparentId(0));
       model.addAttribute("typeList",typeService.findAllByPaging(pCur,typeName,parentId));
       model.addAttribute("counts",typeService.getTotalCount(typeName,parentId));
       model.addAttribute("pCur",(pCur ==null)?1:pCur);
       model.addAttribute("pSize", PageResult.PAGERESULT_PSIZE);
       model.addAttribute("typeName",typeName);
       model.addAttribute("parentId",parentId);
        return "admin/type/type_list";
    }
}
